package com.pokerbean.housingremind.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.pokerbean.housingremind.api.AliSmsApi;
import com.pokerbean.housingremind.common.config.AliyunSmsConfig;
import com.pokerbean.housingremind.entity.SmsSerial;
import com.pokerbean.housingremind.perssist.SmsSerialMapper;
import com.pokerbean.housingremind.service.SmsSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * created by IntelliJ IDEA
 *
 * @author: pokerbean
 * @e-mail: brutalmraz@gmail.com
 * @time: 2018/5/6 10:56
 * @desc: SmsSendServiceImpl
 */
@Service
@Slf4j
public class SmsSendServiceImpl implements SmsSendService {

    @Resource
    SmsSerialMapper smsSerialMapper;

    @Override
    public Map<String, Object> sendMsg(String phoneNo, String time, String content) {
        log.info("sendMsg   in  phone= {}  time ={}  content={} " ,phoneNo,time,content);
        Map<String, Object> result = new HashMap<>();
        try{
            Map<String, Object> sendSms = AliSmsApi.sendSms(phoneNo, time, content);
            SendSmsResponse sendSmsResponse = (SendSmsResponse)sendSms.get("sendSmsResponse");
            log.info(" sendSmsResponse:{}", JSON.toJSONString(sendSmsResponse));
            if (sendSmsResponse != null&& AliyunSmsConfig.REQUEST_OK.equals(sendSmsResponse.getCode())) {
                //  发送成功
                QuerySendDetailsResponse querySendDetailsResponse = AliSmsApi.querySendDetails(sendSmsResponse.getBizId(), phoneNo);
                log.info(" querySendDetailsResponse:{}", JSON.toJSONString(querySendDetailsResponse));

                SmsSerial smsSerialEntity = new SmsSerial();
                smsSerialEntity.setId(UUID.randomUUID().toString().replaceAll("-",""));
                smsSerialEntity.setBizId(sendSmsResponse.getBizId());
                smsSerialEntity.setSmsId(AliyunSmsConfig.smsTemplateCode);
                smsSerialEntity.setSendCode(sendSmsResponse.getCode());
                smsSerialEntity.setQueryCode(querySendDetailsResponse.getCode());
                smsSerialEntity.setSendResponse(JSON.toJSONString(sendSmsResponse));
                smsSerialEntity.setQueryReponse(JSON.toJSONString(querySendDetailsResponse));
                smsSerialMapper.insertSelective(smsSerialEntity);
                if (querySendDetailsResponse != null&&AliyunSmsConfig.QUERY_OK.equals(querySendDetailsResponse.getCode())) {
                    //  短信成功发送到终端
                    result.put("status",0);
                    result.put("msg","发送成功");
                }
            }else{
                result.put("status",1);
                result.put("msg","发送失败");
            }
        }catch(Exception e){
            log.error( "method error： {} ",e.getMessage());
        }
        log.info("sendMsg  out  " );
        return result;
    }

    @Override
    public Map<String, Object> querySendDetails(String bizId, String phoneNo) {
        return null;
    }
}
